package com.carambatv.android.ui.sliding;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.carambatv.android.R;
import com.carambatv.android.adapter.VideoAdapter;
import com.carambatv.android.db.VideoColumns;
import com.carambatv.android.db.VideoQuery;
import com.carambatv.android.model.VideoObject;
import com.carambatv.android.ui.ListScrollListener;

public class VideoFragment extends SherlockFragment implements LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener {
	private ListView mList;
	private CursorAdapter mAdapter;
	private String type;
	private String title;
	public static final int VIDEO_TOKEN = 1;
	public static final String TYPE = "type";
	public static final String TITLE = "title";
	private View root;

	public static VideoFragment newInstance(String type, String title) {
		VideoFragment myFragment = new VideoFragment();

		Bundle args = new Bundle();
		args.putString(TYPE, type);
		args.putString(TITLE, title);
		myFragment.setArguments(args);

		return myFragment;
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.video_list_fragment, container, false);
		mList = (ListView) root.findViewById(R.id.video_list);
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		View ll = new LinearLayout(getActivity());
		mList.addFooterView(ll, null, false);

		
		type = getArguments().getString(TYPE);
		title = getArguments().getString(TITLE);
		
		mAdapter = new VideoAdapter(getActivity(), null);
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(this);
		mList.setOnScrollListener(new ListScrollListener(getActivity(), type, ll));

		Bundle bundle = new Bundle();
		bundle.putString(TYPE, type);
		getActivity().getSupportLoaderManager().initLoader(VIDEO_TOKEN, bundle, this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		getActivity().getSupportLoaderManager().destroyLoader(VIDEO_TOKEN);
		mList.setOnScrollListener(null);
	}

	@Override
	public void onResume() {
		super.onResume();
		getSherlockActivity().getSupportActionBar().setTitle(title);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		Cursor cursor = mAdapter.getCursor();
		if (cursor != null && cursor.moveToPosition(position)) {
			String id_video = cursor.getString(VideoQuery.ID_VIDEO);
			Log.e(id_video, id_video);
			try {
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id_video));
				startActivity(intent);
			} catch (Exception e) {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse("market://details?id=com.google.android.youtube"));
				try {
					startActivity(intent);
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle bundle) {
		String type = bundle.getString(TYPE);
		return new CursorLoader(getActivity(), VideoObject.CONTENT_URI, VideoQuery.PROJECTION, VideoColumns.TYPE.getName() + "=?", new String[] { type }, VideoObject.DATE_SORT);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor newCursor) {
		mAdapter.swapCursor(newCursor);

	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		mAdapter.swapCursor(null);
	}

}
